package com.aiatomic.couting.templates;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.aiatomic.couting.R;
import com.aiatomic.couting.models.TemplateEntity;
import com.aiatomic.couting.util.SystemUtil;
import com.aiatomic.couting.widgets.MyTemplateAdapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

public class MyTemplatesFragment extends Fragment {
	private View view;
	private MyTemplateAdapter adapter;
	private ListView listView;

	public MyTemplateAdapter getAdapter() {
		return adapter;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_my_templates, container, false);
		initUI();
		return view;
	}

	private void initUI() {
		listView = (ListView) view.findViewById(R.id.lv_selected_templates);
		List<TemplateEntity> objects = new ArrayList<TemplateEntity>();
		SystemUtil.getAllLocalStoreTemplates(getContext(),objects);
		adapter = new MyTemplateAdapter(getActivity(), R.layout.list_item_my_template, objects);
		adapter.sort(new Comparator<TemplateEntity>() {
			@Override
			public int compare(TemplateEntity lhs, TemplateEntity rhs) {
				return lhs.getPos() - rhs.getPos();
			}
		});
		listView.setAdapter(adapter);
	}


	public void refresh_local() {
		List<TemplateEntity> objects = new ArrayList<TemplateEntity>();
		SystemUtil.getAllLocalStoreTemplates(getContext(),objects);
		adapter.clear();
		adapter.addAll(objects);
		adapter.notifyDataSetInvalidated();
		adapter.notifyDataSetChanged();
	}
}
